package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.SysuserDao;
import com.xueyu.dao.outRecordDao;
import com.xueyu.dao.personinfoDao;
import com.xueyu.domain.outRecord.outRecordBean;
import com.xueyu.domain.personinfo.personinfoBean;
import com.xueyu.domain.sys.Sysuser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/open-outRecord")
public class outRecordController {

    @Autowired
    outRecordDao outRecordDao;
    @Autowired
    personinfoDao personinfoDao;
    @Autowired
    SysuserDao sysuserDao;

    @RequestMapping(value = "/selectPage-outRecord")
    @ResponseBody
    public Map selectPage(HttpServletRequest req){
        Map map = new HashMap();

        boolean flag = true;

        int current = Integer.parseInt(req.getParameter("current"));
        int pageSize = Integer.parseInt(req.getParameter("size"));
        String search = req.getParameter("searchText");
        String time = req.getParameter("day");
        String beginTime = req.getParameter("beginTime");
        String endTime = req.getParameter("endTime");
        System.out.println("时间"+time);
        System.out.println("从"+beginTime+"到"+endTime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd : HH-mm-ss");

        int count = outRecordDao.countInTime();

        return getMap(map, current, pageSize, search, time, beginTime, endTime, count, flag);
    }

    @RequestMapping(value = "/selectPage2-outRecord")
    @ResponseBody
    public Map selectPage2(HttpServletRequest req){
        Map map = new HashMap();

        boolean flag = false;

        int current = Integer.parseInt(req.getParameter("current"));
        int pageSize = Integer.parseInt(req.getParameter("size"));
        String search = req.getParameter("searchText");
        String time = req.getParameter("day");
        String beginTime = req.getParameter("beginTime");
        String endTime = req.getParameter("endTime");
        System.out.println("时间"+time);
        System.out.println("从"+beginTime+"到"+endTime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd : HH-mm-ss");

        int count = outRecordDao.countOutTime();

        return getMap(map, current, pageSize, search, time, beginTime, endTime, count, flag);
    }

    private Map getMap(Map map, int current, int pageSize, String search, String time, String beginTime, String endTime, int count, boolean flag) {
        if(current <= 0) {
            current = 1;
        }
        if (count == 0){
            outRecordBean outRecordBean = new outRecordBean();
            map.put("count",0);
            map.put("outRecordList",outRecordBean);
            map.put("code",300);
            return map;
        }
        int totalPages = (count + pageSize - 1) / pageSize;
        if(current > totalPages) {
            current = totalPages;
        }
        int offset = (current - 1) * pageSize;
        if (search == "" && search.isEmpty() && time == "" && time.isEmpty() && beginTime == "" && beginTime.isEmpty()) {
            List<outRecordBean> outRecordBeanList;
            if (flag){
                outRecordBeanList = outRecordDao.selectOutRecordAll(null, null, null, null, offset, pageSize);
            }else {
                outRecordBeanList = outRecordDao.selectOutRecordAll2(null, null, null, null, offset, pageSize);
            }
            if (outRecordBeanList.size() == 0){
                map.put("code", 300);
            }else {
                map.put("code",200);
                map.put("outRecordList", outRecordBeanList);
                map.put("count",count);
            }
        } else {
            List<outRecordBean> outRecordBeanList;
            if (flag){
                outRecordBeanList = outRecordDao.selectOutRecordAll(search, time, beginTime, endTime, offset, pageSize);
            }else {
                outRecordBeanList = outRecordDao.selectOutRecordAll2(search, time, beginTime, endTime, offset, pageSize);
            }
            if (outRecordBeanList.size() == 0){
                map.put("code", 300);
            }else {
                map.put("code",200);
                map.put("outRecordList", outRecordBeanList);
                map.put("count",outRecordBeanList.size());
            }
        }
        return map;
    }

    @RequestMapping(value = "/Add-outRecord")
    @ResponseBody
    public Map AddOutRecord(outRecordBean outRecordBean){
        Map map = new HashMap();

        String personName = outRecordBean.getPersonName();

        QueryWrapper<personinfoBean> queryWrapper = new QueryWrapper<personinfoBean>();
        queryWrapper.like("realName",personName);
        List<personinfoBean> personifyBeans = personinfoDao.selectList(queryWrapper);

        List<outRecordBean> recordBeans = outRecordDao.selectWhereName(outRecordBean);
        System.out.println(recordBeans.size());
        if (personifyBeans.size() == 0){
            map.put("code",201);
        } else if (recordBeans.size() != 0) {
            map.put("code",300);
        } else {
            outRecordBean.setPersonId(personifyBeans.get(0).getPersonId());
            int result = outRecordDao.AddOutRecord(outRecordBean);
            if (result > 0){
                map.put("code",200);
                System.out.println("新增true");
            }else {
                map.put("code",202);
                System.out.println("新增false");
            }
        }
        return map;
    }

    @RequestMapping(value = "/getId-outRecord")
    @ResponseBody
    public Map getId(int id,String cmd,String outTime){
        Map map = new HashMap();
        QueryWrapper<outRecordBean> queryWrapper = new QueryWrapper<outRecordBean>();
        if ("toUpdate".equals(cmd)){
            List<outRecordBean> outRecordBeanList = outRecordDao.getIdList(id);
            map.put("getIdList",outRecordBeanList);
        }else if ("delete".equals(cmd)){
            int result = outRecordDao.deleteById(id);
            if (result > 0){
                System.out.println("删除true");
            }else {
                System.out.println("删除false");
            }
        }else if ("toUpdateOutTime".equals(cmd)){
            int result = outRecordDao.UpdateStateAs2(outTime,id);
            if (result > 0){
                System.out.println("修改true");
            }else {
                System.out.println("修改false");
            }
        }
        return map;
    }

    @RequestMapping(value = "/Update-outRecord")
    @ResponseBody
    public Map UpdateOutRecord(outRecordBean outRecordBean){
        Map map = new HashMap();
        int result = outRecordDao.updateById(outRecordBean);
        if (result > 0){
            map.put("code",200);
            System.out.println("修改true");
        }else {
            map.put("code",201);
            System.out.println("修改false");
        }
        return map;
    }

    @RequestMapping(value = "/Login-outRecord")
    @ResponseBody
    public Map LoginOutRecord(String username, String password, int autoONotify){
        Map map = new HashMap();
        QueryWrapper<Sysuser> queryWrapper = new QueryWrapper<Sysuser>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",password);
        List<Sysuser> sysuserList = sysuserDao.selectList(queryWrapper);

        if (sysuserList.size() != 0){
            if (autoONotify == 1){
                Cookie usernameCookie = new Cookie("username",username);
                usernameCookie.setMaxAge(24 * 60 * 60);
                Cookie passwordCookie = new Cookie("password",password);
                passwordCookie.setMaxAge(24 * 60 * 60);
                map.put("code",1);
                map.put("username",usernameCookie);
                map.put("password",passwordCookie);
                map.put("sys",sysuserList);
            }else {
                map.put("code",2);
                map.put("sys",sysuserList);
            }
        }else {
            map.put("code",201);
        }
        return map;
    }
}